package com.yang.dao;

import com.yang.entity.Orderdetail;

import java.sql.*;
import java.util.*;

public class OrderdetailDao {
    //查询全部
    public List<Orderdetail> queryOrderdetail() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Orderdetail");
            rs = ps.executeQuery();
            List<Orderdetail> list = new ArrayList<>();
            while (rs.next()) {
                Orderdetail obj = new Orderdetail();
                obj.setId(rs.getInt("id"));
                obj.setProductname(rs.getString("productname"));
                obj.setProductcrice(rs.getObject("productcrice"));
                obj.setProductcount(rs.getInt("productcount"));
                obj.setOrderid(rs.getString("orderid"));
                obj.setProductid(rs.getInt("productid"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //单个查询
    public Orderdetail queryOrderdetailById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Orderdetail where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Orderdetail obj = new Orderdetail();
                obj.setId(rs.getInt("id"));
                obj.setProductname(rs.getString("productname"));
                obj.setProductcrice(rs.getObject("productcrice"));
                obj.setProductcount(rs.getInt("productcount"));
                obj.setOrderid(rs.getString("orderid"));
                obj.setProductid(rs.getInt("productid"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //添加
    public int saveOrderdetail(Orderdetail obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into orderdetail(productName, productCrice, productCount, orderId, productId) values (?, ?, ?, ?, ?)");
            ps.setString(1, obj.getProductname());
            ps.setObject(2, obj.getProductcrice());
            ps.setInt(3, obj.getProductcount());
            ps.setString(4, obj.getOrderid());
            ps.setInt(5, obj.getProductid());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //删除
    public int deleteOrderdetailById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from Orderdetail where id=?");
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改
    public int updateOrderdetailById(Orderdetail obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("update orderdetail set productName=?, productCrice=?, productCount=?, orderId=?, productId=? where id=?");
            ps.setString(1, obj.getProductname());
            ps.setObject(2, obj.getProductcrice());
            ps.setInt(3, obj.getProductcount());
            ps.setString(4, obj.getOrderid());
            ps.setInt(5, obj.getProductid());
            ps.setInt(6, obj.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
